package org.example.job;

import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.example.common.JsonResult;
import org.example.dao.currency.CurrencyMsgDao;
import org.example.entity.currency.CurrencyMsgEntity;
import org.example.service.currency.CurrencyService;
import org.example.service.utils.Constant;
import org.example.service.utils.okhttp.OkHttpClients;
import org.example.service.utils.okhttp.OkHttpParam;
import org.example.service.utils.okhttp.OkhttpResult;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 货币同步job
 */
@Log4j2
@Component
public class CurrencySyncJob {

    @Resource
    private CurrencyService currencyService;

    @Resource
    private CurrencyMsgDao currencyMsgDao;


    /**
     * 每分钟执行一次,货币等级为一的利率更新
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    private void CurrencyLevelOneRate() {
        log.info("开始每分钟执行一次利率更新");
        OkHttpParam param = new OkHttpParam();
        String api = "https://api.exchangerate-api.com/v4/latest/";
        List<CurrencyMsgEntity> currencyMsgEntities = currencyMsgDao.selectList(
                new QueryWrapper<CurrencyMsgEntity>().eq("use_level", Constant.ONE));
        currencyMsgEntities.forEach(obj -> {
            String string = api + obj.getCurrencyEn();
            OkhttpResult<String> result = null;
            param.setApiUrl(string);
            try {
                result = OkHttpClients.get(param, String.class);
                String apiRes = result.getResult();
                currencyService.syncCurrencyRate(apiRes);
                ThreadUtil.safeSleep(1000);
            } catch (Exception e) {
                log.info("[失败]api:{},返回值:{}", api, result);
                e.printStackTrace();
            }
        });
        log.info("结束每分钟执行一次利率更新");
    }


    /**
     * 每两小时执行一次,货币等级为二的利率更新
     */
    @Scheduled(cron = "0 0 */2 * * ? ")
    private void CurrencyLevelTwoRate() {
        log.info("每两小时执行一次利率更新");
        OkHttpParam param = new OkHttpParam();
        String api = "https://api.exchangerate-api.com/v4/latest/";
        List<CurrencyMsgEntity> currencyMsgEntities = currencyMsgDao.selectList(
                new QueryWrapper<CurrencyMsgEntity>().eq("use_level", Constant.TWO));
        currencyMsgEntities.forEach(obj -> {
            String string = api + obj.getCurrencyEn();
            OkhttpResult<String> result = null;
            param.setApiUrl(string);
            try {
                result = OkHttpClients.get(param, String.class);
                String apiRes = result.getResult();
                currencyService.syncCurrencyRate(apiRes);
                ThreadUtil.safeSleep(3000);
            } catch (Exception e) {
                log.info("[失败]api:{},返回值:{}", api, result);
                e.printStackTrace();
            }
        });
    }

    /**
     * 每天凌晨2点执行一次,货币等级为三的利率更新
     */
    @Scheduled(cron = "0 0 2 * * ? ")
    private void CurrencyLevelThreeRate() {
        log.info("每天凌晨2点执行一次利率更新");
        OkHttpParam param = new OkHttpParam();
        String api = "https://api.exchangerate-api.com/v4/latest/";
        List<CurrencyMsgEntity> currencyMsgEntities = currencyMsgDao.selectList(
                new QueryWrapper<CurrencyMsgEntity>().eq("use_level", Constant.THREE));
        currencyMsgEntities.forEach(obj -> {
            String string = api + obj.getCurrencyEn();
            OkhttpResult<String> result = null;
            param.setApiUrl(string);
            try {
                result = OkHttpClients.get(param, String.class);
                String apiRes = result.getResult();
                currencyService.syncCurrencyRate(apiRes);
                ThreadUtil.safeSleep(3000);
            } catch (Exception e) {
                log.info("[失败]api:{},返回值:{}", api, result);
                e.printStackTrace();
            }
        });
    }
}
